//
//  XXJTabBar.m
//  gosh
//
//  Created by 许小军 on 16/9/22.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#import "XXJTabBar.h"
@interface XXJTabBar ()
{
    UIControl * _previousTabBarButton;
}
@property(nonatomic,strong)UIButton * plusButton;
//@property(weak,nonatomic)UIControl * previousTabBarButton;
@end
@implementation XXJTabBar
-(UIButton *)plusButton
{
    if (_plusButton==nil)
    {
        _plusButton=[[UIButton alloc]init];
        [_plusButton setImage:[UIImage imageNamed:@"tabBar_publish_icon" ]forState:UIControlStateNormal];
        [_plusButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon" ] forState:UIControlStateHighlighted];
        [_plusButton sizeToFit];
        [self addSubview:_plusButton];
    }
    return _plusButton;
}


//- (UIButton *)plusButton
//{
//    if (_plusButton == nil) {
//        
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
//        [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
//        [btn sizeToFit];
//        [self addSubview:btn];
//        
//        _plusButton = btn;
//    }
//    return _plusButton;
//}

-(void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger count=self.items.count;
    
    CGFloat btnW=self.cur_w/(count+1);
    CGFloat btnH=self.cur_h;
    CGFloat x=0;
    int i=0;
    //(UIView * tabBarBtn in self.subviews)按钮增加点击事件需要改为UIControl，UIView的父类是UIControl
    for (UIControl * tabBarBtn in self.subviews)
    {
        if ([tabBarBtn isKindOfClass:NSClassFromString(@"UITabBarButton")])
        {
            if (_previousTabBarButton==nil && i==0) {
                _previousTabBarButton=tabBarBtn;
            }
            
            if (i==2)
            {
                i+=1;
            }
            x=i*btnW;
            tabBarBtn.frame=CGRectMake(x, 0, btnW, btnH);
            i++;
            [tabBarBtn addTarget:self action:@selector(tabBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    
    //self.plusButton.frame=CGRectMake(self.cur_w*0.5, self.cur_h*0.5, btnW, btnH);
    self.plusButton.center=CGPointMake(self.cur_w/2, self.cur_h/2);
    
}


-(void)tabBarBtnClick:(UIControl *)tabBarBtn
{
    if (_previousTabBarButton==tabBarBtn) {
        //NSLog(@"tabBar双击");
        
        [[NSNotificationCenter defaultCenter]postNotificationName:TabBarDoubleClicked object:nil];
    }
    _previousTabBarButton=tabBarBtn;
}

@end
